1816B - Grid Reconstruction - CodeForces Solution


constructive algorithms greedy *1000

Please click on ads to support us..

Python Code:

def arrayBuilder(n):
  list = []
  for i in range(2 * n):
    list.append(i + 1)
  for k in range(n // 2):
    list[n + 2 * k] = 2 + 2 * k
  for j in range(n - 1):
    list[j + 1] = list[j + n] - 1
  list[0] = 2 * n - 1

  output = ""
  for num in list:
    output += str(num) + " "
    if (num == n - 1):
      output += "\n"
  print(output)

testCases = int(input())
for q in range(testCases):
  u = int(input())
  if u%2 == 0:
    arrayBuilder(u)

C++ Code:

#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using ld = long double;
using pll = pair<ll,ll>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vstr = vector<string>;
using vpll = vector<pll>; 
 
#define tcT template<class T
#define tcTU tcT, class U
 
#define pb push_back
#define fi first
#define se second
#define all(x) begin(x), end(x)

// loops
#define FOR(i,a,b) for (ll i = (a); i < (b); ++i)
#define ROF(i,a,b) for (ll i = (b)-1; i >= (a); --i)
#define trav(a,x) for (auto& a: x)

//min heap
template<class T> using pqg = priority_queue<T,vector<T>,greater<T>>;

 
const ll MOD = 1e9+7; // 998244353;
const ll MX = 2e5+5;
const ll INF = 1e18; // not too close to LLONG_MAX
const ld PI = acos((ld)-1);
const ll dx[4] = {1,0,-1,0}, dy[4] = {0,1,0,-1}; // for every grid problem!!
map<pll,char> dirFromDiff = {{{1,0},'R'},{{0,1},'U'},{{-1,0},'L'} , {{0,-1},'D'}};
map<char,pll> diffFromDir = {{'R',{1,0}} , {'U',{0,1}}, {'L',{-1,0}},{'D',{0,-1}}};


#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<class T> 
using oset = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;

tcTU> T fstTrue(T lo, T hi, U f) {hi ++; assert(lo <= hi); while (lo < hi) {T mid = lo+(hi-lo)/2;
  f(mid) ? hi = mid : lo = mid+1; } return lo;}
tcTU> T lstTrue(T lo, T hi, U f){lo --; assert(lo <= hi);while (lo < hi) {T mid = lo+(hi-lo+1)/2;
  f(mid) ? lo = mid : hi = mid-1;} return lo;}

ll binpow(ll x,ll n){if(n==0)return 1;ll temp = binpow(x,n/2);temp  =(temp*temp)%MOD;
  if(n&1) {temp = (temp*x)%MOD;}return temp;}

void solve(){
  ll n;  cin>>n;

  ll maxi = 2*n, mini = 2;
  vector<vll> ans(2,vll(n));

  FOR(j,0,n){
    if(j%2==0){
      ans[0][j] = maxi;
      maxi -=2;
    }
    if(j%2==1){
      ans[0][j] = mini;
      mini+=2;
    }
  }
  maxi = 2*n-1;
  mini = n-1;
  for(ll j = n-1;j>=0;--j){
    if(j%2==1){
      ans[1][j] = maxi;
      maxi -=2;
    }
    if(j%2==0){
      ans[1][j] = mini;
      mini-=2;
    }
  }

  
  FOR(i,0,2){
    FOR(j,0,n){
      cout<<ans[i][j]<<" ";
    }
    cout<<"\n";
  }
}

int main(){
  cin.sync_with_stdio(0);	cin.tie(0);	cout.tie(0);
  ll t=1;
  cin>>t;
  FOR(i,0,t){
    solve();
  }
}


Comments

Submit
0 Comments
More Questions

302A - Eugeny and Array
1638B - Odd Swap Sort
1370C - Number Game
1206B - Make Product Equal One
131A - cAPS lOCK
1635A - Min Or Sum
474A - Keyboard
1343A - Candies
1343C - Alternating Subsequence
1325A - EhAb AnD gCd
746A - Compote
318A - Even Odds
550B - Preparing Olympiad
939B - Hamster Farm
732A - Buy a Shovel
1220C - Substring Game in the Lesson
452A - Eevee
1647B - Madoka and the Elegant Gift
1408A - Circle Coloring
766B - Mahmoud and a Triangle
1618C - Paint the Array
469A - I Wanna Be the Guy
1294A - Collecting Coins
1227A - Math Problem
349A - Cinema Line
47A - Triangular numbers
1516B - AGAGA XOOORRR
1515A - Phoenix and Gold
1515B - Phoenix and Puzzle
155A - I_love_username